<?php

namespace App\HttpController\Admin;

use App\HttpController\AdminBase;
use EasySwoole\Validate\Validate;
use  App\Model\Menu as MenuModel;

/**
 * 菜单
 * Class Menu
 * @package App\HttpController\Admin
 */
class Menu extends AdminBase
{


    /**
     * 菜单模型
     * @return MenuModel
     */
    public function getMenuModel()
    {
        return new MenuModel();
    }

    /**
     * 菜单
     * @return string|void
     */
    public function index()
    {
        $menu_list = $this->getMenuModel()->getMenuList([], 'title as name,pid as pId,id,icon');
        $this->assign(['menu_list' => json_encode($menu_list)]);
        return $this->fetch('index');
    }

    /**
     * 编辑
     * @return \EasySwoole\Mysqli\Mysqli|mixed|null
     */
    public function edit()
    {
        if ($this->isPost()) {
            $id = $this->postParam('id');
            $res=$this->getMenuModel()->getInfo(['id' => $id]);
            $titleName=$this->getMenuModel()->getFiledValue(['id' => $res['pid']],'title');
            $res['pname']=empty($titleName)?'顶级菜单':$titleName;
            return $this->writeJson('操作成功',$res );
        }
    }

    /**
     * 添加数据
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function save()
    {
        $data = $this->postParams();
        $data['pid'] = empty($data['pid']) ? 0 : $data['pid'];
        return $this->writeJson('操作成功', $this->getMenuModel()->updateOrCreateData($data, ["id" => $this->postParam("id")]));
    }

    /**
     * 删除数据
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function del()
    {
        return $this->writeJson('操作成功', $this->getMenuModel()->delData(["id" => $this->postParam("id")]));
    }

    /**
     * 生成树形
     * @param type $list
     * @param type $level
     * @param type $title
     * @return type
     * @author  <278805354@qq.com>
     */
    private function _toFormatTree($list, $level = 0, $title = 'title')
    {
        foreach ($list as $key => $val) {
            $tmp_str = str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;", $level * 2);
            $tmp_str .= "└";

            $val['level'] = $level;
            $val['title_show'] = $level == 0 ? $val[$title] . "&nbsp;" : $tmp_str . $val[$title] . "&nbsp;";
            // $val['title_show'] = $val['id'].'|'.$level.'级|'.$val['title_show'];
            if (!array_key_exists('_child', $val)) {
                array_push($this->formatTree, $val);
            } else {
                $tmp_ary = $val['_child'];
                unset($val['_child']);
                array_push($this->formatTree, $val);
                $this->_toFormatTree($tmp_ary, $level + 1, $title); //进行下一层递归
            }
        }
        return;
    }

    /**
     * 选择顶级菜单
     * @return string|void
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\Option
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function show_select_menus()
    {
        $menu_list = $this->getMenuModel()->getMenuList([], 'title as name,pid as pId,id,icon');
        $this->assign(['menu_list' => json_encode($menu_list)]);
        return $this->fetch();
    }

    /**
     * 选择图标
     * @return string|void
     */
    public function select_icon()
    {
        return $this->fetch('select_icon');
    }

    /**
     * 提交参数验证
     * @param string|null $action
     * @return Validate|null
     */
    protected function getValidateRule(?string $action): ?Validate
    {
        $validate = null;
        return $validate;
    }
}